﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration.Provider;
using System.Configuration;
using System.Collections.Specialized;
using NLBS.Entity;
using System.Collections;
using NLBS.Framework;
using NLBS.Provider;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace NLBS.Data
{
    public abstract class DbProvider : ProviderBase
    {
        
        private static DbProvider provider;
        public Database db;
        private static GenericProviderCollection<DbProvider> providers = ProviderConfigurationHelper.LoadProviderCollection<DbProvider>("Database", out provider);

        /// <summary>
        /// Returns the currently configured DbProvider.
        /// </summary>
        /// <returns></returns>
        public static DbProvider Instance()
        {
            return provider;
        }


        public override void Initialize(string name, NameValueCollection configValue)
        {
            base.Initialize(name, configValue);
        }

        #region sqlhelp

        protected int ExecuteNonQuery(string query)
        {
            return SqlHelper.ExecuteNonQuery(query, db);
        }

        public int ExecuteNonQuery(string query, DbParameter[] parameters)
        {
            return SqlHelper.ExecuteNonQuery(query, db, parameters);
        }

        protected IDataReader ExecuteReader(string query)
        {
            return SqlHelper.ExecuteReader(query, db);
        }

        protected IDataReader ExecuteReader(string query, DbParameter[] parameters)
        {
            return SqlHelper.ExecuteReader(query, db, parameters);
        }

        protected int ExecuteScalar(string query)
        {
            return Convert.ToInt32(SqlHelper.ExecuteScalar(query, db));
        }

        protected object ExecuteScalar(string query, DbParameter[] parameters)
        {
            return SqlHelper.ExecuteScalar(query, db, parameters);
        }

        #endregion

        private string _connectionString;
        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value></value>
        public virtual string ConnectionString
        {
            //TODO: Make this protected.
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        #region Category
        /// <summary>
        /// 获取分类列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<Category> GetCategory();

        /// <summary>
        /// 根据分类的Id获取一个分类对象
        /// </summary>
        /// <param name="catID">分类的ID</param>
        /// <returns></returns>
        public abstract Category GetCategoryByID(int catID);
        /// <summary>
        /// 新增一个分类
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public abstract int InsertCategory(Category cat);
        
        /// <summary>
        /// 更新一个分类
        /// </summary>
        /// <param name="cat">分类的ID</param>
        public abstract void UpdateCategory(Category cat);
        
        /// <summary>
        /// 根据分类的ID删除一个分类
        /// </summary>
        /// <param name="catID"></param>
        public abstract void DeleteCategory(int catID);

        /// <summary>
        /// 转移日志到指定的分类
        /// </summary>
        /// <param name="catIDList">分类列表</param>
        /// <param name="newCatID">新的分类</param>
        public abstract void MoveCategory(string catIDList, int newCatID);

        public abstract void UpdateCategoryCounter(int catID);
        
        #endregion

        #region Article

        public abstract IList<Article> GetArticle();

        /// <summary>
        /// 获取日志前后日志
        /// </summary>
        /// <param name="articleID">日志ID</param>
        /// <returns></returns>
        public abstract IList<ArticleLink> GetPreviousAndNextArticle(int articleID);

        /// <summary>
        /// 获取最新日志
        /// </summary>
        /// <param name="topCount"></param>
        /// <returns></returns>
        public abstract IList<Article> GetTopArticle(int topCount);

        /// <summary>
        /// 根据用户ID获取日志
        /// </summary>
        /// <param name="startIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetArticleByUserID(int startIndex, int pageSize, int userID);

        /// <summary>
        /// 分页获取日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetAllArticleByPage(int startIndex, int pageSize);

        public abstract IPagedCollection<Article> GetRssArticleByPage(int pageSize);

        /// <summary>
        /// 根据分类分页获取日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <param name="catID">分类ID</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetCatArticleByPage(int startIndex, int pageSize, int catID);

        /// <summary>
        /// 获取精选日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetSelectedArticleByPage(int startIndex, int pageSize);
        /// <summary>
        /// 根据日志的ID获取一篇日志
        /// </summary>
        /// <param name="articleID">日志ID</param>
        /// <returns></returns>
        public abstract Article GetArticleByID(int articleID);

        /// <summary>
        /// 新增一条日志
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public abstract int InsertArticle(Article item);

        /// <summary>
        /// 更新一条日志
        /// </summary>
        /// <param name="item"></param>
        public abstract void UpdateArticle(Article item);

        public abstract void UpdateArticleViewCount(int articleId);

        /// <summary>
        /// 根据日志的ID删除日日志
        /// </summary>
        /// <param name="articleID"></param>
        public abstract void DeleteArticle(int articleID);

        public abstract int GetArticleCategoryCount(int catID);

        /// <summary>
        /// 根据月份获取日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetPagedArticleByMonth(int startIndex, int pageSize,int year,int month);

        /// <summary>
        /// 根据年份获取日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <param name="year">年</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetPagedArticleByYear(int startIndex, int pageSize, int year);

        /// <summary>
        /// 根据具体某天获取日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <param name="date">当前时间</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetPagedArticleByDay(int startIndex, int pageSize, DateTime date);

        /// <summary>
        /// 获取日志归档
        /// </summary>
        /// <returns></returns>
        public abstract IList<Archive> GetArticleMonthArchive();

        public abstract IList<Article> GetAllArticleByMonth(int year,int month);

        /// <summary>
        /// 根据关键字搜索日志
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <param name="prefix">关键字</param>
        /// <returns></returns>
        public abstract IPagedCollection<Article> GetPagedArticleBySearch(int startIndex, int pageSize, string prefix);

        /// <summary>
        /// 在更新时更新日志总数
        /// </summary>
        /// <param name="oldCatID">日志原分类ID</param>
        /// <param name="catID">现在日志ID</param>
        /// <param name="authorID">作者ID</param>
        public abstract void UpdateArticleCount(int oldCatID,int catID,int authorID);


        #endregion
        
       // #region Links

       // public abstract IList<Link> GetLink();

       // public abstract Link GetLinkByID(int catID);

       // public abstract void InsertLink(Link item);

       // public abstract void UpdateLink(Link item);

       // public abstract void DeleteLink(int linkID);
        
       // #endregion
        
       // #region Images

       // public abstract IList<Image> GetImage();

       // public abstract Image GetImageByID(int imageID);

       // public abstract void InsertImage(Image item);

       // public abstract void UpdateImage(Image item);

       // public abstract void DeleteImage(int imageID);
        
       // #endregion

        #region Comment

        public abstract IList<Comment> GetCommentList();

        public abstract IPagedCollection<Comment> GetCommentBySearch(int startIndex, int pageSize, string prefix);

        /// <summary>
        /// 获取日志评论
        /// </summary>
        /// <param name="articleID"></param>
        /// <returns></returns>
        public abstract IList<Comment> GetCommentListByArticleID(int articleID);

        /// <summary>
        /// 获取用户评论
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public abstract IList<Comment> GetCommentListByUserID(int userID);

        /// <summary>
        /// 获取一个评论
        /// </summary>
        /// <param name="commID"></param>
        /// <returns></returns>
        public abstract Comment GetCommentByID(int commID);

        /// <summary>
        /// 添加一条评论
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        public abstract int InsertComment(Comment comm);

        /// <summary>
        /// 更新一个评论
        /// </summary>
        /// <param name="comm"></param>
        public abstract void UpdateComment(Comment comm);

        /// <summary>
        /// 删除一条评论
        /// </summary>
        /// <param name="commID"></param>
        public abstract void DeleteComment(int commID);

        /// <summary>
        /// 获取最新评论
        /// </summary>
        /// <param name="intCount"></param>
        /// <returns></returns>
        public abstract IList<Comment> GetTopComment(int intCount);

        /// <summary>
        /// 获取评论总数
        /// </summary>
        /// <returns></returns>
        public abstract int GetAllCommentCount();

        /// <summary>
        /// 分页获取评论
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示评论数</param>
        /// <returns></returns>
        public abstract IPagedCollection<Comment> GetAllCommentByPage(int startIndex, int pageSize);

        /// <summary>
        /// 更新评论数
        /// </summary>
        /// <param name="logID"></param>
        /// <param name="authorID"></param>
        public abstract void UpdateCommentCount(int logID,int authorID);
        #endregion

        #region GuestBook

        /// <summary>
        /// 获取留言列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<GuestBook> GetGuestBook();

        /// <summary>
        /// 分页根据关键字搜索评论
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示留言数</param>
        /// <param name="prefix">关键字</param>
        /// <returns></returns>
        public abstract IPagedCollection<GuestBook> GetGuestBookBySearch(int startIndex, int pageSize, string prefix);

        /// <summary>
        /// 获取最新留言
        /// </summary>
        /// <returns></returns>
        public abstract IList<GuestBook> GetTopGuestBook();

        /// <summary>
        /// 获取单条留言
        /// </summary>
        /// <param name="gbookID"></param>
        /// <returns></returns>
        public abstract GuestBook GetGuestBookByID(int gbookID);

        /// <summary>
        /// 新增一条留言
        /// </summary>
        /// <param name="gbook"></param>
        /// <returns></returns>
        public abstract int InsertGuestBook(GuestBook gbook);

        /// <summary>
        /// 更新一条留言
        /// </summary>
        /// <param name="gbook"></param>
        public abstract void UpdateGuestBook(GuestBook gbook);

        /// <summary>
        /// 删除一条留言
        /// </summary>
        /// <param name="gbookID"></param>
        public abstract void DeleteGuestBook(int gbookID);

        /// <summary>
        /// 获取留言总数
        /// </summary>
        /// <returns></returns>
        public abstract int GetAllGuestBookCount();

        /// <summary>
        /// 分页获取留言
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示留言数</param>
        /// <returns></returns>
        public abstract IPagedCollection<GuestBook> GetAllGuestBookByPage(int startIndex, int pageSize);

        #endregion

        #region Smile

        /// <summary>
        /// 获取表情列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<Smile> GetSmile();

        /// <summary>
        /// 获取一个表情
        /// </summary>
        /// <param name="smID"></param>
        /// <returns></returns>
        public abstract Smile GetSmileByID(int smID);

        /// <summary>
        /// 添加一个表情
        /// </summary>
        /// <param name="sm"></param>
        /// <returns></returns>
        public abstract int InsertSmile(Smile sm);

        /// <summary>
        /// 更新一个表情
        /// </summary>
        /// <param name="sm"></param>
        public abstract void UpdateSmile(Smile sm);

        /// <summary>
        /// 删除一个表情
        /// </summary>
        /// <param name="smID"></param>
        public abstract void DeleteSmile(int smID);

        #endregion

        #region settings

        /// <summary>
        /// 获取blog参数列表
        /// </summary>
        /// <returns></returns>
        public abstract Dictionary<string, Setting> GetSettings();

        /// <summary>
        /// 更新blog参数
        /// </summary>
        /// <param name="set"></param>
        public abstract void UpdateSettings(Setting set);
        #endregion

        #region User
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<User> GetUser();
        /// <summary>
        ///  根据用户ID获取用户
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns></returns>
        public abstract User GetUserByID(int userID);
        /// <summary>
        /// 根据用户名获取用户
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns></returns>
        public abstract User GetUserByName(string userName);
        /// <summary>
        /// 新增一个用户
        /// </summary>
        /// <param name="user">待新增用户对象</param>
        public abstract void InsertUser(User user);
        /// <summary>
        /// 更新一个用户
        /// </summary>
        /// <param name="user">待更新用户对象</param>
        public abstract void UpdateUser(User user);

        /// <summary>
        /// 根据用户的ID删除一个用户
        /// </summary>
        /// <param name="userID">待删除用户ID</param>
        public abstract void DeleteUser(int userID);
        /// <summary>
        /// 验证用户的用户名和密码
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public abstract bool ValidateUser(string username, string password);


        #endregion

        #region UserGroup
        /// <summary>
        /// 获取用户组列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<UserGroup> GetUserGroup();
        /// <summary>
        /// 新增一个用户组
        /// </summary>
        /// <param name="group">要新增的用户组</param>
        /// <returns></returns>
        public abstract int InsertUserGroup(UserGroup group);
        /// <summary>
        /// 更新一个用户组
        /// </summary>
        /// <param name="group">要更新的用户组</param>
        public abstract void UpdateUserGroup(UserGroup group);

        /// <summary>
        /// 根据用户组ID获取一个用户组
        /// </summary>
        /// <param name="groupID">用户组ID</param>
        /// <returns></returns>
        public abstract UserGroup GetUserGroupByID(int groupID);
        /// <summary>
        /// 根据用户组ID删除一个用户组
        /// </summary>
        /// <param name="groupID">用户组ID</param>
        public abstract void DeleteUserGroup(int groupID);


        #endregion

        #region WordFilter

        public abstract IList<WordFilter> GetWordFilter();

        public abstract WordFilter GetWordFilterByID(int itemID);

        public abstract int InsertWordFilter(WordFilter item);

        public abstract void UpdateWordFilter(WordFilter item);

        public abstract void DeleteWordFilter(int itemID);

        #endregion

        #region record

        /// <summary>
        /// 获取访客信息
        /// </summary>
        /// <returns></returns>
        public abstract IList<VisitorRecord> GetVisitorRecord();

        /// <summary>
        /// 添加一条访客信息
        /// </summary>
        /// <param name="visitor"></param>
        public abstract void InsertVisitorRecord(VisitorRecord visitor);

        #endregion

        #region links
        /// <summary>
        /// 获取友情链接
        /// </summary>
        /// <returns></returns>
        public abstract IList<FriendLink> GetLinks(LinkItemType linkType);

        /// <summary>
        /// 获取特定链接
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public abstract FriendLink GetLinkByID(int linkID);

        /// <summary>
        /// 添加友情链接
        /// </summary>
        /// <param name="link"></param>
        public abstract void InsertLink(FriendLink link);

        /// <summary>
        /// 更新友情链接
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public abstract int UpdateLink(FriendLink link);

        /// <summary>
        /// 删除友情链接
        /// </summary>
        /// <param name="link"></param>
        /// <returns></returns>
        public abstract int DeleteLink(int linkID);

        #endregion

        #region misc

        public abstract void UpdateGlobalCounter();

        public abstract void UpdateCategoryCounter();

        public abstract void UpdateArticleCounter();

        public abstract void UpdateUserCounter();

        public abstract void CleanInactiveUserAccount();

        public abstract void CleanVisitorCounter();

        public abstract void CleanGuestBookEntries();


        #endregion

        #region trackback

        /// <summary>
        /// 获取引用列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<TrackBack> GetTrackBack();

        /// <summary>
        /// 分页根据关键字搜索引用
        /// </summary>
        /// <param name="startIndex">起始页</param>
        /// <param name="pageSize">每页显示日志数</param>
        /// <param name="prefix">关键字</param>
        /// <returns></returns>
        public abstract IPagedCollection<TrackBack> GetTrackBackBySearch(int startIndex, int pageSize, string prefix);

        public abstract void InsertTrackBack(TrackBack tb);

        public abstract TrackBack GetTrackBackByID(int id);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="excerpt"></param>
        /// <returns></returns>
        public abstract TrackBack GetTrackBackByName(string title, string excerpt);

        /// <summary>
        /// 更新一条引用信息
        /// </summary>
        /// <param name="tb"></param>
        public abstract void UpdateTrackBack(TrackBack tb);
        /// <summary>
        /// 删除一条引用
        /// </summary>
        /// <param name="id"></param>
        public abstract void DeleteTrackBack(int id);


        #endregion

        #region Database

        public abstract string BackUpDatabase(string backuppath, string ServerName, string UserName, string Password, string strDbName, string strFileName);

        public abstract string RestoreDatabase(string backuppath, string ServerName, string UserName, string Password, string strDbName, string strFileName);

        #endregion

        #region tag

        public abstract IList<string> GetTagByID(Article art);

        public abstract ITagCollection<string, Tag> GetTags();

        public abstract int SaveTag(Article art);

        public abstract int DeleteTag(Article art);

        public abstract IPagedCollection<Article> GetArticleByTag(int startIndex, int pageSize, string tag);

        public abstract IList<Article> GetArticleByIDArray(string[] artIDArray);

        public abstract IList<string> GetArtIDByTag(string tag);
        #endregion

        #region widget

        /// <summary>
        /// 获取Widget列表
        /// </summary>
        /// <returns></returns>
        public abstract IList<Widget> GetWidget();

        /// <summary>
        /// 添加一个Widget
        /// </summary>
        /// <param name="widget"></param>
        /// <returns></returns>
        public abstract int InsertWidget(Widget widget);

        /// <summary>
        /// 更新Widget
        /// </summary>
        /// <param name="widget"></param>
        /// <returns></returns>
        public abstract int UpdateWidget(Widget widget);

        /// <summary>
        /// 删除一个Widget
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public abstract int DeleteWidget(int id);

        /// <summary>
        /// 获取一个Widget
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public abstract Widget GetWidgetDetail(int id);

        #endregion

        #region ExtensionLink

        public abstract IList<ExtensionLink> GetExtensionLink(int type);

        public abstract IList<ExtensionLink> GetExtensionLink();

        public abstract int InsertExtensionLink(ExtensionLink link);

        public abstract int UpdateExtensionLink(ExtensionLink link);

        public abstract int DeleteExtensionLink(int id);

        public abstract ExtensionLink GetExtensionLinkDetail(int id);

        #endregion
    }
}
